/**
 * New node file
 */

var confirmFriend = function(id, userId1, userId2, options){
	this.id = id;
	this.userId1 = userId1;
	this.userId2 = userId2;
	this.error = options.error;
	this.success = options.success;
	this.confirm();
};

confirmFriend.prototype._error = function(code, message){
	if(typeof this.error == 'function') {
		this.error(code, message);
	}
};

confirmFriend.prototype._success = function(data){
	if(typeof this.success == 'function') {
		this.success(data);
	}
}

confirmFriend.prototype.check = function(success){
	var self = this;
	Mysql.query("SELECT COUNT(id) FROM Friend WHERE areFriend=1 AND id="+Mysql.escape(self.id)+" LIMIT 1").on('error', function(error){
		console.log('\nmysql error: '+error);
		self._error(9999, 'mysql error');
	}).on('result', function(result){
		if(result['COUNT(id)'] == 0) {
			self._error(9999, 'Not found request');
			return;
		}
		success();
	});
};

confirmFriend.prototype.confirm = function(){
	var self = this;
	self.check(function(){
		var data = {};
		data.id = self.id;
		data.userId1 = self.userId1;
		data.userId2 = self.userId2;
		Mysql.query("UPDATE Friend SET areFriend=2 WHERE id="+Mysql.escape(self.id))
		.on('error', function(error){
			console.log('\nmysql error '+error);
			self._error(9999, 'mysql error');
		})
		.on('result', function(result){
			data.areFriend = 2;
			self._success(data);
		});
	});
};

module.exports = confirmFriend;